home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
C⁄C++
/
Pop Up Menu CDEF
/
Pop Up.note
< prev
Wrap
Text File
|
1995-06-11
|
9KB
|
190 lines
// Pop Up menu CDEF
// (C) 1990-1995 Stuart Cheshire <cheshire@cs.stanford.edu>
// Written after endless frustration with the one by Chris Faigle which
// used to crash all the time, to whom some credit for inspiration should go.
INTRODUCTION
To use Stuart's Pop Up menu CDEF you need to
1. Paste the CDEF resource (about 3K) into your application's resource file
2. Create the menus you want to pop up, just like normal menu bar menus
3. Add Pop Up Menu Control items (CNTLs) to your dialogs
In use, the popup menu controls behave just like any other control. They
automatically respond to mouse clicks, they have maximum and minimum
values just like a scroll bar control, and they have a current value which
your program can read with GetCtlValue or change with SetCtlValue. The
minimum value of a popup menu control is 1, and the maximum value is the
number of items in the menu.
There are three components to a popup menu control: The title (name) of
the control, the menu that will pop up when clicked upon, and the little
piece of code (the CDEF) that makes it all work.
The way you tell the Macintosh these three pieces of information is with
a CNTL resource. Having created a CNTL resource (described below) which
specifies the control's title, MENU, CDEF, and other sundry formatting
information about how it should appear on the screen, you can then add
"Control" items to your dialogs just as you would for scroll bars and
other controls. When you create the "Control" item in ResEdit, enter
the resource id of your CNTL resource that describes that control. Each
different control in your dialog should have its own CNTL describing it,
but they may share the same menus, if appropriate.
Logically, the layout of the information looks like this:
DLOG ---> DITL
Button ---> CDEF
Check box /
Control -------> CNTL ---> MENU
Radio button \
Radio button ---> Title & other layout information
etc.
A popup menu control is displayed as two components, the title part, on
the left, and the pop up menu part, on the right.
______________________
Control Title | Selected Menu Item |
~~~~~~~~~~~~~~~~~~~~~~
When these instructions talk about the width of the title, they are
referring to the part on the left. When they talk about the width of
the menu, they are referring to the part on the right. When they talk
about the width of the control, then are referring to the total width
of both parts.
CREATING THE CNTL
The fields of the CNTL resource are interpreted in the following (slightly
non-standard) way:
BoundsRect: The overall size of the pop up control (both the title and the
menu). Ususally, top = 0, left = 0, bottom = 16, right = the width
Value: Initial default menu selection (ie 1 for the first menu item).
Visible: TRUE or FALSE, as you wish
Max: Width of the title of the pop up menu. (Use zero for automatic size.)
Min: Resource ID of the menu to use
ProcID: CDEF resource to use. Formula is Resource ID * 16 + variation code
If this CDEF resource id is 2, ProcID should be 32 + variation code
RefCon: Resource type (optional, see below)
Title: The title string to display for this control (optional).
Note: the menu's own title is ignored. This means that you can
make a single menu containing items "slow", "medium" and "fast",
and then use that menu in multiple places in the same dialog,
with different control titles for each one, like "ship speed",
"bullet speed", "enemy speed" etc.
Variation codes:
Bit 0 set if you want the menu width stretched so that the menu goes all
the way to the right edge of the control rectangle
Bit 1 set for "New style" (with little arrow pointing downwards)
Bit 2 set to build the menu dynamically by calling AddResMenu(RefCon)
Bit 3 set to right-justify the title (only applies if contrlMax is non-zero)
SPECIAL FEATURES
* Can automatically build a pop up resource menu -- set bit 2 of the variation
code and set RefCon to the resource type. Eg. set RefCon to the value 'FONT'
(464F4E54 hex or 1179602516 decimal) to build a font menu.
* If you specify a contrlMin of zero, instead of reading a menu resource,
the CDEF will let you create a menu programatically at runtime and then
put its handle in the Control RefCon. (This is of dubious functionality,
but Chris Faigle's original CDEF supported it, so I did too.)
* After reading the initialization parameters out of the Min and Max fields,
they are set to 0 and the number of items in the menu, respectively. This
means that SetCtlValue, GetCtlMin, and GetCtlMax calls in your program
will work correctly.
* The CDEF correctly responds to calcCntlRgn calls etc. which means that
you get an accurate preview when constructing your dialogs in ResEdit.
* Pop up control title is automatically highlighted when the menu is
clicked on. No need to do this yourself in your program.
* Multiple CNTLs can reference the same MENU. For example, you might
want to do this if your program has a dialog with three options, each
of which offer the user the choice of "Small, medium, large". With most
other Pop Up Menu CDEFs, you have to create three identical menus because
otherwise they will crash when they dispose of the same menu three times.
Since the same MENU resource can be used in multiple places, the title
defined with the menu (if any) is not displayed. The title which is
displayed is the control title defined in the CNTL resource.
* Supports both "command" and "option" oriented pop ups.
"Option" oriented pop ups (fonts, sizes, baud rates etc.) are basically
just space-saving replacements for radio button groups, so only one item
from the menu may be selected at any one time, and what is displayed in
the drop-shadow box is whatever the currently selected item is.
"Command" oriented pop ups are menus where selecting an item from them
performs some command instead of just changing the control's value.
For these kind of pop ups where the control does not have any particular
single "value", use SetCtlValue(theControl, 0) and the CDEF will display
the menu's title in the box instead of any of the items. This is not a
good way to use pop-up menus, but may be useful in certain restricted
circumstances where you are otherwise contstrained and cannot make a
nicer user interface. For instance, Intercon's NFS/Share software uses
this technique in order to place a popup menu in the Chooser from which
the user can select commands "Add host", "Delete host", "Edit host" etc.
* Disabled pop ups (HiliteControl(theControl, 255)) are correctly drawn
greyed out.
* Allows alignment of columns of popup menus, including right-justification
of titles if desired. For example:
DEFAULT STYLE, "Max" is zero, for automatic sizing of titles
_________
Tank Icons | Small |
~~~~~~~~~
__________
Pillbox Icons | Medium |
~~~~~~~~~~
______________
Refueling Base Icons | Super Huge |
~~~~~~~~~~~~~~
FIXED WIDTH TITLE STYLE, "Max" is set by hand to width of widest title
_________
Tank Icons | Small |
~~~~~~~~~
__________
Pillbox Icons | Medium |
~~~~~~~~~~
______________
Refueling Base Icons | Super Huge |
~~~~~~~~~~~~~~
RIGHT-JUSTIFIED FIXED WIDTH TITLE STYLE,
"Max" is set by hand to width of widest title and
Bit 3 (right-justify title) of variation code is set
_________
Tank Icons | Small |
~~~~~~~~~
__________
Pillbox Icons | Medium |
~~~~~~~~~~
______________
Refueling Base Icons | Super Huge |
~~~~~~~~~~~~~~
* Allows fixed width menus. Similarly to the right justification of titles
above, if you have a column of different sized menus, setting bit 2 of the
variation code tells the CDEF to extend the width of the menu up to the
right edge of the control's bounding box so that all the menus appear
pleasingly aligned on the screen. For example:
RIGHT-JUSTIFIED FIXED WIDTH TITLE STYLE WITH FIXED WIDTH MENU,
"Max" is set by hand,
Bit 3 (right-justify title) and
Bit 0 (fixed-width menu) of variation code are set
_______________
Tank Icons | Small |
~~~~~~~~~~~~~~~
_______________
Pillbox Icons | Medium |
~~~~~~~~~~~~~~~
_______________
Refueling Base Icons | Super Huge |
~~~~~~~~~~~~~~~